module.exports = class user_mod extends require('./model') {
  /**
   * @description: 根据输入内容搜索相关类型指定数量的用户
   * @param {*} type
   * @param {*} inputText
   * @param {*} charType
   * @param {*} pageSize
   * @param {*} currPage
   * @return {*}
   */
  static getUsersByTypeAndCharMod(type, inputText, charType, pageSize = 3, currPage) {
    pageSize = Number(pageSize)
    currPage = Number(currPage)
    let startIndex = (currPage - 1) * pageSize
    return new Promise((resolve, reject) => {
      let sql = `select * from user where ${charType} like "%${inputText.trim()}%" and type = ${type} limit ${startIndex},${pageSize}`

      this.query(sql).then(res => resolve(res))
        .catch(err => reject(err))

    })
  }

  /**
   * @description: 获取相关关键字的所有用户
   * @param {*} type
   * @param {*} inputText
   * @param {*} charType
   * @return {*}
   */
  static getUsersByTypeAndCharTotal(type, inputText, charType) {
    return new Promise((resolve, reject) => {
      let sql = `select count(1) count from user where ${charType} like "%${inputText.trim()}%" and type = ${type} `

      this.query(sql).then(res => resolve(res))
        .catch(err => reject(err))

    })
  }

  // 发布通知
  static announceMod(title, classes, content) {
    return new Promise((resolve, reject) => {
      let sql = "insert into `notice`(title,class,content) values(?,?,?)"
      this.query(sql, [title, classes, content]).then(res => {
        resolve("发布成功")
      }).catch(err => {
        reject("由于网络原因,你的公告没有发出~~~")
      })
    })
  }

  // 获取所有的通知
  static getAllNoticeMod(pageSize, currPage) {
    pageSize -= 0;
    currPage -= 0;
    let startIndex = (currPage - 1) * pageSize
    return new Promise((resolve, reject) => {
      let sql = "select * from `notice` order by create_time desc limit ?,?"
      this.query(sql, [startIndex, pageSize]).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

  // 获取所有公告的数量
  static getAllNoticeTotal() {
    return new Promise((resolve, reject) => {
      let sql = "select count(1) count from `notice`"
      this.query(sql).then(res => {
        resolve(res)
      }).catch(err => {
        reject("由于网络原因,获取数量失败")
      })
    })
  }

  // 获取所有班级
  static getAllClassesMod(pageSize, currPage) {
    let sql, startIndex
    return new Promise((resolve, reject) => {
      if (pageSize == undefined) {
        sql = "select * from classes"
      } else {
        startIndex = (currPage - 1) * pageSize
        pageSize -= 0;
        sql = `select * from classes limit ${startIndex},${pageSize} `
      }
      this.query(sql).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

  // 模糊查询班级
  static getClassesSearchMod(className, pageSize, currPage) {
    let sql, startIndex
    return new Promise((resolve, reject) => {
      if (pageSize == undefined) {
        sql = `select * from classes where class like "%${className}%"`
      } else {
        startIndex = (currPage - 1) * pageSize
        pageSize -= 0;
        sql = `select * from classes where class like "%${className}%" limit ${startIndex},${pageSize} `
      }
      this.query(sql).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

  // 添加班级
  static addClassesMod(classes) {
    return new Promise(async (resolve, reject) => {
      let sql = `select * from classes where class = "${classes}"`
      let res = await this.query(sql)
      if (res.length) resolve('已存在该班级，请不要重复添加')
      else {
        sql = `insert into classes(class) values("${classes}")`
        this.query(sql).then(res => {
          resolve("添加成功")
        }).catch(err => {
          reject("添加失败")
        })
      }

    })
  }

  // 删除所有公告
  static delNoticeMod(n_id) {
    return new Promise((resolve, reject) => {
      let sql = `delete from notice where n_id = ${n_id}`
      this.query(sql).then(res => {
        resolve("success")
      }).catch(err => {
        reject(err)
      })
    })
  }

  // 删除涉及的阅读表
  static delReadByNoticeMod(n_id) {
    return new Promise((resolve, reject) => {
      let sql = "delete from `read` where n_id = " + n_id
      this.query(sql).then(res => {
        resolve("success")
      }).catch(err => {
        reject(err)
      })
    })
  }
}